package me.springboot.mybatis.zhcs.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 时间工具类
 */
public class DateUtils {
    private static final String FORMAT_DATE = "yyyy-MM-dd";
    private static final String FORMAT_DATE_TIME = "yyyy-MM-dd HH:mm:ss";

    public static String getLongToDate(Long millSec){
        SimpleDateFormat sdf = new SimpleDateFormat(FORMAT_DATE);
        Date date= new Date(millSec);
        return sdf.format(date);
    }

    public static String getLongToDateTime(Long millSec){
        SimpleDateFormat sdf = new SimpleDateFormat(FORMAT_DATE_TIME);
        Date date= new Date(millSec);
        return sdf.format(date);
    }

    /**
     * 获取时间字符串
     * @param date
     * @return
     */
    public static String parseDate(Date date){
        SimpleDateFormat sdf = new SimpleDateFormat(FORMAT_DATE_TIME);
        return sdf.format(date);
    }

    /**
     * 根据时间字符串和具体格式转换成Date类型
     * @param dateStr
     * @param format
     * @return
     */
    public static Date parseString(String dateStr,String format) {
        Date date = null;
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        try {
            date = sdf.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 根据字符串转换成Date类型 yyyy-MM-dd格式
     * @param dateStr
     * @return
     */
    public static Date parseDateByString(String dateStr){
        return parseString(dateStr, FORMAT_DATE);
    }
    /**
     * 根据字符串转换成Date类型 yyyy-MM-dd HH:mm:ss格式
     * @param dateStr
     * @return
     */
    public static Date parseDateTimeByString(String dateStr){
        return parseString(dateStr, FORMAT_DATE_TIME);
    }

    public static Date parseDateByYearMonthDay(int year, int month, int day){
        String str = year + "-" + month +"-" + day;
        return parseDateByString(str);
    }

    /**
     * 格式化Date到字符串
     * @param date
     * @param format
     * @return
     */
    public static String formatDate(Date date, String format) {
        if(date == null || format == null || "".equals(format)){
            return "";
        }
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        return formatter.format(date);
    }
    public static String formatToDate(Date date){
        return formatDate(date, FORMAT_DATE);
    }
    public static String formatToDateTime(Date date){
        return formatDate(date, FORMAT_DATE_TIME);
    }
}
